import GChild from "../../../Core/GView/GChild";
import { Res } from "../../Common/UIResources";
import GameMgr from "../../Logic/GameMgr";
import { JXBattleAnalysis } from "../JXBattle/JXBattleAnalysis";
import JXResultHero from "./JXResultHero";


const { ccclass, property, menu, executeInEditMode } = cc._decorator;
@ccclass @menu('View/Fight/BtHeroHurt')

export default class BtHeroHurt extends GChild {
    @property({ type: cc.Node, tooltip: "英雄头像" }) icon: cc.Node = null;
    @property(cc.Label) hurtNum: cc.Label = null;
    @property(cc.Label) heroName: cc.Label = null;

    private _ans: JXBattleAnalysis = null;
    private _heroId: number = null;
    private _show: boolean = false;
    public setView(id, ans: JXBattleAnalysis, heroId: number) {
        let rHero = GameMgr.rHeroData.getHero(id);
        if (!rHero) {
            this.node.removeFromParent();
            this.node.destroy();
            return;
        }
        this._heroId = id;
        if (this._heroId == heroId) {
            this._show = true;
        }
        this._ans = ans;
        /**英雄头像 */
        this.setHeroHead();
    }
    protected setHeroHead() {
        this.assetImpl.prefab(Res.prefab.vw.fight.resultHero, (prefab: cc.Prefab) => {
            this._ans.defTPL.partnerData.forEach((v: IHeroBtlData, k: number) => {
                if (v.tableId == this._heroId) {
                    let node = cc.instantiate(prefab);
                    let comp = node.getComponent(JXResultHero);
                    let hurt = this._ans.heroHurdInfo.get(v.tableId);
                    hurt = hurt ? hurt : 0;
                    let COINPAREM: COINPAREM = {
                        coin: hurt.toString(),
                        add: hurt.toString()
                    }
                    /**伤害值 */
                    let name = GameMgr.heroData.getRaw<SHeroDataRaw>(v.tableId).name;
                    this.heroName.string = name + '';
                    this.onAddCoin(COINPAREM);
                    comp.setView(v.tableId, 0);
                    comp.closeExp();
                    if (this._show) {
                        comp.showKing();
                    }
                    this.icon.addChild(node);
                }
            })
        })
    }

    /**战力滚动 */
    public onAddCoin(param: COINPAREM) {
        // console.log("coin:" + JSON.stringify(param));
        var self = this;
        let constCoin = parseInt(param.coin) - parseInt(param.add);
        var obj = { a: 0 };
        this.hurtNum.string = '' + param.coin;
        cc.tween(obj).to(0.3, { a: 100 }, {
            progress: (start, end, current, ratio) => {
                if (self.node) self.hurtNum.string = constCoin + Math.floor(param.add * ratio);
            }
        }).start();
        // if (param.add > 0) cc.tween(this.nodeIcon).to(0.1, { scale: 1.2 }).to(0.2, { scale: 1 }).start();
    }

}